package level4Assessment;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CreditCardValidation 
{
	private String ccExpiryDate;
	private String ccCardNum;
	public String getCcExpiryDate() {
		return ccExpiryDate;
	}

	public String getCcCardNum() {
		return ccCardNum;
	}

	boolean CCValid = false;
	public boolean isCCValid() {
		return CCValid;
	}

	boolean CCNum = false;
	boolean CCDate = false;
	
 public String ccValidation()
 {
	 try
	{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		SimpleDateFormat date = new SimpleDateFormat("MM/yy");
		Date today = new Date();
		String todaysString = date.format(today);
		Date todaysDate = date.parse(todaysString);
		Date ccExpiryCheck;
		
		while(CCValid == false)
		{
			System.out.println("Please Enter Your Credit Card Number");
			ccCardNum = in.readLine();
			ccCardNum = ccCardNum.trim();
			
			if(ccCardNum.length() != 16)
			{
				System.out.println("Card Number is not Valid, Please Enter a 16 Digit Card Number");
				return "Card Number is not Valid, Please Enter a 16 Digit Card Number";
		
			}else
			{
				System.out.println("Card Number is Valid");
				CCNum = true;
			while(CCDate == false)
			{
				System.out.println("Please Enter Your Cards Expiry Date");
				ccExpiryDate = in.readLine();
				ccExpiryDate = ccExpiryDate.trim();
				ccExpiryCheck = date.parse(ccExpiryDate);
				
				if(ccExpiryDate.length() == 5 && ccExpiryCheck.after(todaysDate))
				{
					System.out.println("Expiry Date is Valid");
					CCDate = true;
					CCValid = true;
				}else
				{
					System.out.println("Expiry Date is not Valid");
					return "Expiry Date is not Valid";
				}
			}
		}
	}
	}catch(IOException e)
		{
			System.out.println(e);
		}catch(ParseException p)
		{
			System.out.println(p);
		}
	 return null;
 }
}
